//
//  WYHttpServer+WYProfile.m
//  CirclePush
//
//  Created by admin on 2018/5/22.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYHttpServer+WYProfile.h"

@implementation WYHttpServer (WYProfile)
+(void)getUserInfoSucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetUserInfo WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}
+(void)getMediaListWithHistoryOrCollect:(NSString *)historyOrCollect WithPageNum:(NSString *)pageNum WithPageSize:(NSString *)pageSize SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    
    [parm setValue:historyOrCollect forKey:@"historyOrCollect"];
    [parm setValue:pageNum forKey:@"pageNum"];
    [parm setValue:pageSize forKey:@"pageSize"];
    
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetCollectionList WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
    
}
+(void)addFeedWithWay:(NSString *)contactWay WithCoupleBack:(NSString *)coupleBack SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    
    [parm setValue:contactWay forKey:@"contactWay"];
    [parm setValue:coupleBack forKey:@"coupleBack"];
   // [parm setValue:pageSize forKey:@"pageSize"];
    
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlAddFeed WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
}
+(void)getSystemMessageWithPageNum:(NSString *)pageNum WithPageSize:(NSString *)pageSize SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    
    [parm setValue:pageNum forKey:@"pageNum"];
    [parm setValue:pageSize forKey:@"pageSize"];
    
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetOneMessage WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}

+(void)getClewListWithState:(NSString *)state WithPageNum:(NSString *)pageNum WithPageSize:(NSString *)pageSize SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    if (state.length) {
        [parm setValue:state forKey:@"state"];
    }
    [parm setValue:pageNum forKey:@"pageNum"];
    [parm setValue:pageSize forKey:@"pageSize"];
    
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetClewList WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
    
    
}

+(void)getClewInfoSucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetClewCimInfo WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
}
+(void)getExtractListWithState:(NSString *)state WithPageNum:(NSString *)pageNum WithPageSize:(NSString *)pageSize SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:state forKey:@"state"];
    [parm setValue:pageNum forKey:@"pageNum"];
    [parm setValue:pageSize forKey:@"pageSize"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlGetExtractList WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
    
    
    
}

+(void)upLoadImage:(NSData *)data SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"file"]=@"headImage.png";
//    NSString *messStr=[NSString stringWithFormat:@"%@%@",JJUDURL,JJUDuserInfosave];
    //formData: 专门用于拼接需要上传的数据,在此位置生成一个要上传的数据体
    [manager POST:kUrlUploadImage parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //        UIImage *image =[UIImage imageNamed:@"moon"];
       // NSData *data = UIImagePNGRepresentation(image);
        // 在网络开发中，上传文件时，是文件不允许被覆盖，文件重名
        // 要解决此问题，
        // 可以在上传时使用当前的系统事件作为文件名
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
        //上传
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 对应网站上[upload.php中]处理文件的[字段"file"]
         3. 要保存在服务器上的[文件名]
         4. 上传文件的[mimeType]
         */
        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //上传进度
        // @property int64_t totalUnitCount;     需要下载文件的总大小
        // @property int64_t completedUnitCount; 当前已经下载的大小
        //
        // 给Progress添加监听 KVO
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        // 回到主队列刷新UI,用户自定义的进度条
        //        dispatch_async(dispatch_get_main_queue(), ^{
        //            self.progressView.progress = 1.0 *
        //            uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
        //        });
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [self reloadview];
//        NSLog(@"上传成功 %@", responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
        if (![dic isKindOfClass:[NSDictionary class]]) {
            HUDNormal(@"服务器闹点小情绪,努力安抚中,请稍候~");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenLoading" object:nil];
            return;
        }
        
        NSLog(@"%@",dic);
      
        
        if ([dic[@"code"] integerValue]==0) {
            completion(YES,dic,nil);
        }else{
            completion(NO,nil,dic[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(NO,nil,@"上传失败");
        NSLog(@"上传失败 %@", error);
    }];
}
+(void)AddTwitterWithName:(NSString *)name WithTelephone:(NSString *)telephone WithBankCard:(NSString *)bankCard WithCardFront:(NSString *)cardFront WithCartVerso:(NSString *)cartVerso WithBankRight:(NSString *)bankRight WithBankback:(NSString *)bankback SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    
    
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:name forKey:@"name"];
    [parm setValue:telephone forKey:@"telephone"];
    [parm setValue:bankCard forKey:@"bankCard"];
    [parm setValue:cardFront forKey:@"cardFront"];
    [parm setValue:cartVerso forKey:@"cartVerso"];
    [parm setValue:bankRight forKey:@"bankRight"];
    [parm setValue:bankback forKey:@"bankBack"];
    

    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlAddTwitter WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}

+(void)addCollectionWithMediaId:(NSString *)mediaId SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:mediaId forKey:@"mediaId"];
    
    
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlInsertCollect WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}
+(void)delegateCollectionWithMediaId:(NSString *)mediaId SucessBlock:(void(^)(BOOL success,id response,NSString *errorMessage))completion
{
    AFHTTPSessionManager *manager=[[WYHttpServer sharedInstance] requestManager];
    NSMutableDictionary *parm=[[WYHttpServer sharedInstance] requestParameters];
    [parm setValue:mediaId forKey:@"mediaId"];
    [[WYHttpServer sharedInstance] postOperationWithManger:manager Url:kUrlDeleteCollect WithPara:parm Success:^(BOOL success, id response, NSString *errorMsg) {
        completion(success,response,errorMsg);
    }];
}

@end
